package cn.blog.controller.front;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.origin.User;
import cn.blog.service.front.CompileHeadService;
import cn.blog.service.front.CompileRightService;

@WebServlet("/front/compileright")
public class Compile_right extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CompileHeadService chs = new CompileHeadService();
		// 获取用户id
		String userId = req.getSession().getAttribute("userId").toString();
		int uid = Integer.parseInt(userId);
		// 根据用户编号获取文章信息
		User userInfo = chs.getUser(uid);
		req.setAttribute("user", userInfo);
		forward("front/compileright");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CompileHeadService chs = new CompileHeadService();
		// 获取用户id
		String userId = req.getSession().getAttribute("userId").toString();
		int uid = Integer.parseInt(userId);
		// 根据用户编号获取文章信息
		User userInfo = chs.getUser(uid);

		CompileRightService crs = new CompileRightService();

		// 修改昵称和性别
		String uname = req.getParameter("changeName");
		if (uname == null || uname == "") {
			uname = userInfo.getName();
		}
		String sex = req.getParameter("changeSex");
		System.out.println(uname + sex);
		int changeSex;
		if (sex == "" || sex == null) {
			changeSex = userInfo.getSex();
		} else {
			changeSex = sex.equals("男") ? 0 : 1;
		}

		int i = crs.update(uname, changeSex, uid);

		// 修改电话，QQ号，邮箱
		String phone = req.getParameter("changePhone");
		if (phone == null || phone == "") {
			phone = userInfo.getPhone();
		}
		String qq = req.getParameter("changeQQ");
		if (qq == null || qq == "") {
			qq = userInfo.getQq();
		}
		String email = req.getParameter("ChangeEmail");
		if (email == null || email == "") {
			email = userInfo.getEmail();
		}
		crs.update(phone, qq, email, uid);
		req.setCharacterEncoding("utf-8");
		
		//修改头像
		String touxiang = req.getParameter("touxiang");
		if (touxiang == null || touxiang == "") {
			touxiang = userInfo.getImage();
		}
		int num = crs.update(touxiang, uid);

		if (num == 1) {
			req.setAttribute("label", "上传成功");
		}

		redirect("/front/compileright");
	}
}
